package com.wjs.CarolSecKill.exception;

import com.wjs.CarolSecKill.vo.RespBean;
import com.wjs.CarolSecKill.vo.RespBeanEnum;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler  {
    @ExceptionHandler(Exception.class)
    public RespBean ExceptionHandler(Exception e){
        if(e instanceof GlobalException){
            return RespBean.error(((GlobalException) e).getRespBeanEnum());
        }else if(e instanceof BindException){
            BindException ex=(BindException) e;
            RespBean respBean = RespBean.error(RespBeanEnum.BINDING_ERROR);
            respBean.setMessage("参数校验异常"+ex.getAllErrors().get(0).getDefaultMessage());
            return respBean;
        }else{
            return RespBean.error(RespBeanEnum.ERROR);
        }
    }
}
